<template>
  <div class="page">
    <el-form :model="searchForm" label-position="left" inline label-suffix="：" size="small">
      <div class="flex wap flex-start">
        <el-form-item label="手机号">
          <el-input v-model="searchForm.phone" maxlength="11" clearable></el-input>
        </el-form-item>
        <el-form-item label="学生">
          <!-- <item-select :itemId="searchForm.stuid" @checkedItem="checkedsearchStu" type='stu'></item-select> -->
          <check-stu :itemId="searchForm.stuid" @checkedItem="checkedsearchStu"/>
        </el-form-item>
        <el-form-item label="老师">
          <!-- <item-select :itemId="searchForm.empid" @checkedItem="checkedsearchEmp" type='emp'></item-select> -->
          <check-emp :itemId="searchForm.empid" @checkedItem="checkedsearchEmp"/>
        </el-form-item>

        <el-form-item label="">
          <el-button type="primary" @click="getData">查询</el-button>
        </el-form-item>
      </div>
    </el-form>
    <div class="flex wap flex-start" style="padding: 0px 0 10px 0">
      <el-button type="success" @click="addUser">新增用户</el-button>
      <div style="width:20px"></div>
    </div>
    <el-table :data="tableData" border style="width: 100%"  height="600"  :header-cell-style="{
        background: '#eef1f6',
        color: '#606266',
        'text-align': 'center'
      }"
      :cell-style="{ 'text-align': 'center' }">
      <!-- <el-table-column prop="userid" label="用户ID"></el-table-column> -->
      <el-table-column prop="phone" label="手机号" width="150"></el-table-column>
      <el-table-column prop="rolename" label="角色" width="100"></el-table-column>
      <el-table-column prop="empid" label="关联教师/学生">
        <template slot-scope="scop">
          <span v-if="scop.row.roleid==1">{{scop.row.stuname+'-'+scop.row.stuphone}}</span>
          <span v-if="scop.row.roleid==2">{{scop.row.empname+'-'+scop.row.empphone}}</span>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"></el-table-column>
      <el-table-column prop="headimg" label="头像" width="200">
        <template slot-scope="scop">
          <el-image fit="fil" style="width: 80px; height: 50px" :src="scop.row.headimg.indexOf('http')>-1?scop.row.headimg:$Url + '/upload/' + scop.row.headimg"
            :preview-src-list="[scop.row.headimg.indexOf('http')>-1?scop.row.headimg:$Url + '/upload/' + scop.row.headimg]">
          </el-image>
        </template>
      </el-table-column>
      <el-table-column prop="" label="操作" width="250">
        <template slot-scope="scop">
          <el-button type="primary" icon="el-icon-edit" circle @click="edit(scop.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="del(scop.row.userid)"></el-button>
          <el-button type="warning" slot="reference" plain size="small" @click="resetPas(scop.row.userid)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="float:right;padding:10px">
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :page-sizes="[10, 20, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper" :total="total">
      </el-pagination>
    </div>
    <el-dialog title="新增/更新用户" center :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="userForm" ref="addUserForm" :rules="rule" label-width="100px" class="edit-form" label-suffix="：">
        <el-form-item label="手机号" prop="phone">
          <el-input type="text" placeholder="请输入手机号" maxlength="11" v-model="userForm.phone" :disabled="userForm.userid!=''"></el-input>
        </el-form-item>
        <el-form-item label="密码" v-if="!userForm.userid" prop="password">
          <el-input type="text" placeholder="请输入密码" v-model="userForm.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="确认密码" v-if="!userForm.userid" prop="secpassword">
          <el-input type="text" placeholder="请再次输入密码" v-model="userForm.secpassword" show-password></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleid">
          <el-radio-group v-model="userForm.roleid" :disabled="userForm.userid!=''">
            <el-radio :label="2">老师</el-radio>
            <el-radio :label="1">学生</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="选择老师" prop="empid" v-if="userForm.roleid == 2">
          <!-- <item-select :itemId="userForm.empid" @checkedItem="checkedEmp" type='emp'></item-select> -->
          <check-emp :itemId="userForm.empid" @checkedItem="checkedEmp"/>
        </el-form-item>
        <el-form-item label="选择学生" prop="stuid" v-if="userForm.roleid == 1">
          <!-- <item-select :itemId="userForm.stuid" @checkedItem="checkedStu" type='stu'></item-select> -->
          <check-stu :itemId="userForm.stuid" @checkedItem="checkedStu"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveAdd">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { findUsers, addUser, updateUser, resetPassword, delUser } from "@/api/user";
import checkStu from "@/views/components/checkStu";
import checkEmp from "@/views/components/checkEmp";
export default {
  components: {
    checkStu,
    checkEmp,
  },
  data() {
    var again = (rule, value, callback) => {
      if (value !== this.userForm.password) {
        callback(new Error('两次输入的密码不一致，请重试'))
      } else {
        callback()
      }
    };
    return {
      dialogFormVisible: false,
      total: 0,
      pageNum: 1,
      pageSize: 10,
      tableData: [],
      searchForm: {
        phone: "",
        empid: '',
        stuid: '',
      },
      userForm: {
        userid: '',
        phone: '',
        password: '',
        roleid: '',
        secpassword: '',
        empid: '',
        stuid: ''
      },
      rule: {
        phone: [
          { required: true, message: '手机号必须填写', trigger: 'change' }
        ],
        password: [
          { required: true, message: '请设置密码', trigger: 'change' }
        ],
        secpassword: [
          { validator: again, trigger: 'change' },
          { required: true, message: '请再次输入  密码', trigger: 'change' }
        ],
        roleid: [
          { required: true, message: '请设置角色', trigger: 'change' }
        ],
        empid: [
          { required: true, message: '请设置老师', trigger: 'change' }
        ],
        stuid: [
          { required: true, message: '请设置学生', trigger: 'change' }
        ]
      }

    };
  },
  computed: {
    queryData() {
      return {
        phone: this.searchForm.phone,
        empid: this.searchForm.empid,
        stuid: this.searchForm.stuid,
        pageNum: this.pageNum,
        pageSize: this.pageSize
      };
    }
  },
  mounted() {
    this.getData();
  },
  methods: {
    async getData() {
      const res = await findUsers.post(this.queryData);
      if (res.resultCode === "000000") {
        this.tableData = res.data.userList;
        this.total = res.data.count ? res.data.count : 0;
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    addUser() {
      this.dialogFormVisible = true
      this.$nextTick(() => {
        this.$refs['addUserForm'].resetFields();
      })
      this.userForm = {
        userid: '',
        phone: '',
        password: '',
        roleid: '',
        secpassword: '',
        empid: '',
        stuid: ''
      }
    },
    edit(row) {
      console.log(row, 55);
      this.dialogFormVisible = true
      const { userid, phone, password, secpassword, roleid, empid, stuid } = row
      this.userForm = {
        userid,
        phone,
        password: '',
        roleid,
        secpassword: '',
        empid,
        stuid
      }
    },
    checkedEmp(val) {
      this.userForm.empid = val
    },
    checkedStu(val) {
      this.userForm.stuid = val
    },
    checkedsearchEmp(val) {
      this.searchForm.empid = val
    },
    checkedsearchStu(val) {
      this.searchForm.stuid = val
    },
    async saveAdd() {
      this.$refs.addUserForm.validate(async (valid) => {
        if (valid) {
          if (this.userForm.userid) {
            const res = await updateUser.post(this.userForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          } else {
            const res = await addUser.post(this.userForm);
            if (res.resultCode === "000000") {
              this.$message.success(res.resultInfo);
              this.dialogFormVisible = false
              this.getData()
            } else {
              this.$message.error(res.resultInfo);
            }
          }
        }
      })
    },
    del(userid) {
      this.$confirm("此操作将删除该项目, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.delUser(userid)
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消删除"
        });
      });
    },
    async delUser(userid) {
      const res = await delUser.post({ userid: userid });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    resetPas(userid) {
      this.$confirm("此操作将重置密码, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.respsw(userid)
      }).catch(() => {
        this.$message({
          type: "info",
          message: "已取消"
        });
      });

    },
    async respsw(userid) {
      const res = await resetPassword.post({ userid: userid });
      if (res.resultCode === "000000") {
        this.$message.success(res.resultInfo);
        this.getData()
      } else {
        this.$message.error(res.resultInfo);
      }
    },
    handleSizeChange(val) {
      this.pageNum = 1;
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.pageNum = val;
      this.getData();
    }
  }
};
</script>
<style lang="less" scoped></style>
